/**********************************************************
 * OverviewAction.java
 * Created by Ankur
 * Last modified Date: Sep 17, 2008 3:57:47 PM by Ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.P13n;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class OverviewAction.
 */
public class OverviewAction extends FrontBaseSupport implements ServletRequestAware{

	/** The Constant logger. */
	private static final Logger logger = Logger.getLogger(OverviewAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The user. */
	private User user;

	private P13n p13n;

	private List<Items> itemsList = new ArrayList<Items>();

	private List<Items> itemsWatchList = new ArrayList<Items>();

	/** The request. */
	private HttpServletRequest request;

	public OverviewAction(){
		super(true, false);
		setMyAccount(true);
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}
		user = (User)request.getSession().getAttribute(AttributeConstants.USER);
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("user.id", user.getId());
		List<P13n> p13list = (List<P13n>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, P13n.class);

		if(p13list != null && p13list.size() > 0) {
			this.p13n = p13list.get(0);
		} else {
			this.p13n = new P13n();
		}

		if(request.getParameter("parse") != null && request.getParameter("parse").equals("true")) {
			if(this.p13n.getPurchased()) {
				BuyingAction buyingAction = new BuyingAction();
				buyingAction.buyingOverviewPurchased(request, itemsList);
			}

			if(this.p13n.getWatching()) {
				BuyingAction buyingAction = new BuyingAction();
				buyingAction.buyingOverviewWatching(request, itemsWatchList);
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end");
		}
		return SUCCESS;
	}

	public String addOverviewModule() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}


		if(request.getParameter("module") != null && request.getParameter("module").trim().length() > 0) {
			user = (User)request.getSession().getAttribute(AttributeConstants.USER);
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("user.id", user.getId());
			List<P13n> p13list = (List<P13n>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, P13n.class);

			if(p13list != null && p13list.size() > 0) {
				this.p13n = p13list.get(0);
			} else {
				this.p13n = new P13n();
				this.p13n.setUser(user);
			}

			if(request.getParameter("module").equals("overview")) {
				this.p13n.setOverview(true);
			} else if(request.getParameter("module").equals("purchased")) {
				this.p13n.setPurchased(true);
			} else if(request.getParameter("module").equals("watching")) {
				this.p13n.setWatching(true);
			}

			if(p13list != null && p13list.size() > 0) {
				CommonFunctionDelegator.generalUpdatebyobj(this.p13n);
			} else {
				CommonFunctionDelegator.generalInsertbyobj(this.p13n);
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end");
		}
		return execute();
	}

	public String remove1OverviewModule() throws Exception {
		return removeOverviewModule(1);
	}

	public String remove2OverviewModule() throws Exception {
		return removeOverviewModule(2);
	}

	public String remove3OverviewModule() throws Exception {
		return removeOverviewModule(3);
	}

	public String removeOverviewModule(int module) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}


		user = (User)request.getSession().getAttribute(AttributeConstants.USER);
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.setAttribute("user.id", user.getId());
		List<P13n> p13list = (List<P13n>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, P13n.class);

		if(p13list != null && p13list.size() > 0) {
			this.p13n = p13list.get(0);
		} else {
			this.p13n = new P13n();
			this.p13n.setUser(user);
		}

		if(module == 1) {
			this.p13n.setOverview(false);
		} else if(module == 2) {
			this.p13n.setWatching(false);
		} else if(module == 3) {
			this.p13n.setPurchased(false);
		}

		if(p13list != null && p13list.size() > 0) {
			CommonFunctionDelegator.generalUpdatebyobj(this.p13n);
		} else {
			CommonFunctionDelegator.generalInsertbyobj(this.p13n);
		}


		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end");
		}
		return execute();
	}


	/* (non-Javadoc)
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	/**
	 * Gets the user.
	 *
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * Sets the user.
	 *
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	public P13n getP13n() {
		return p13n;
	}

	public List<Items> getItemsList() {
		return itemsList;
	}

	public List<Items> getItemsWatchList() {
		return itemsWatchList;
	}
}